package main;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.Action;
import javax.swing.SwingConstants;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.BorderLayout;
import javax.swing.JMenuItem;



public class main_window {


	private JFrame frmWineGameWine;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					main_window window = new main_window();
					window.frmWineGameWine.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public main_window() {
		try {
			  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch(Exception e) {
			  System.out.println("Error setting native LAF: " + e);
			}
		values.setIDGame(0);
		values.setNGame("Select/Create Game");
		values.setWeek(0);
		timer = new Timer();
		initialize();
		 timer.scheduleAtFixedRate(new RemindTask(), 0, //initial delay
			        15 * 1000); //subsequent rate
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		ImageIcon img = null;
		try {
			img = new ImageIcon(new URL(connect.Dev_URL() + "icon.png"));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		frmWineGameWine = new JFrame();
		frmWineGameWine.setIconImage(img.getImage());
		frmWineGameWine.setTitle("Wine Game Wine Supply Chain Council");
		frmWineGameWine.setBounds(100, 100, 920, 707);
		frmWineGameWine.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SpringLayout springLayout = new SpringLayout();
		SplashScreen nn;
		try {
			nn = new SplashScreen(new URL(connect.Dev_URL() + "splash2.png"), frmWineGameWine, 2000);
			nn.setVisible(true);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		frmWineGameWine.getContentPane().setLayout(springLayout);
		
		JMenuBar menuBar = new JMenuBar();
		springLayout.putConstraint(SpringLayout.NORTH, menuBar, 0, SpringLayout.NORTH, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, menuBar, 0, SpringLayout.WEST, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, menuBar, 21, SpringLayout.NORTH, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, menuBar, 0, SpringLayout.EAST, frmWineGameWine.getContentPane());
		frmWineGameWine.getContentPane().add(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		springLayout.putConstraint(SpringLayout.WEST, tabbedPane, 0, SpringLayout.WEST, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, tabbedPane, 0, SpringLayout.EAST, menuBar);
		frmWineGameWine.getContentPane().add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Game Management", null, panel, null);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);
		
		JLabel lblOrderManagement = new JLabel("Game Control");
		lblOrderManagement.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_panel.putConstraint(SpringLayout.NORTH, lblOrderManagement, 10, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, lblOrderManagement, 26, SpringLayout.WEST, panel);
		panel.add(lblOrderManagement);
		
		JPanel panel_6 = new JPanel();
		sl_panel.putConstraint(SpringLayout.NORTH, panel_6, 6, SpringLayout.SOUTH, lblOrderManagement);
		sl_panel.putConstraint(SpringLayout.WEST, panel_6, 10, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, panel_6, 86, SpringLayout.SOUTH, lblOrderManagement);
		sl_panel.putConstraint(SpringLayout.EAST, panel_6, -10, SpringLayout.EAST, panel);
		panel_6.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Step 1: Create/Control Game", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(panel_6);
		panel_6.setLayout(null);
		
		JButton btnNewButton = new JButton("Create Game");
		btnNewButton.setBounds(31, 31, 102, 23);
		panel_6.add(btnNewButton);
		btnNewButton.setAction(action);

		
		JButton btnNewButton_1 = new JButton("Open Existing Game");
		btnNewButton_1.setAction(action_1);
		btnNewButton_1.setBounds(155, 31, 129, 23);
		panel_6.add(btnNewButton_1);
		
		JLabel lblCurrentGameIn = new JLabel("Current Game in Control :");
		lblCurrentGameIn.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblCurrentGameIn.setBounds(320, 35, 153, 14);
		panel_6.add(lblCurrentGameIn);
		
		lblNewLabel = new JLabel("" + values.getNGame());
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblNewLabel.setBounds(483, 35, 227, 14);
		panel_6.add(lblNewLabel);
		
		JPanel panel_1 = new JPanel();
		sl_panel.putConstraint(SpringLayout.NORTH, panel_1, 13, SpringLayout.SOUTH, panel_6);
		sl_panel.putConstraint(SpringLayout.WEST, panel_1, 10, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, panel_1, 147, SpringLayout.SOUTH, panel_6);
		sl_panel.putConstraint(SpringLayout.EAST, panel_1, 0, SpringLayout.EAST, panel_6);
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Step 2: Players", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("Winery :");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblNewLabel_1.setBounds(37, 23, 80, 14);
		panel_1.add(lblNewLabel_1);
		
		lblStatus_1 = new JLabel("Status");
		lblStatus_1.setBounds(127, 24, 46, 14);
		panel_1.add(lblStatus_1);
		
		JLabel lblImporter = new JLabel("Importer :");
		lblImporter.setHorizontalAlignment(SwingConstants.RIGHT);
		lblImporter.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblImporter.setBounds(37, 48, 80, 14);
		panel_1.add(lblImporter);
		
		label_1 = new JLabel("Status");
		label_1.setBounds(127, 49, 46, 14);
		panel_1.add(label_1);
		
		JLabel lblDistributor = new JLabel("Distributor :");
		lblDistributor.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDistributor.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblDistributor.setBounds(37, 73, 80, 14);
		panel_1.add(lblDistributor);
		
		label_3 = new JLabel("Status");
		label_3.setBounds(127, 74, 46, 14);
		panel_1.add(label_3);
		
		JLabel lblRetailer = new JLabel("Retailer :");
		lblRetailer.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRetailer.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblRetailer.setBounds(37, 98, 80, 14);
		panel_1.add(lblRetailer);
		
		label_5 = new JLabel("Status");
		label_5.setBounds(127, 99, 46, 14);
		panel_1.add(label_5);
		
		JLabel label = new JLabel("Winery :");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label.setBounds(318, 23, 80, 14);
		panel_1.add(label);
		
		label_2 = new JLabel("Status");
		label_2.setBounds(408, 24, 126, 14);
		panel_1.add(label_2);
		
		JLabel label_4 = new JLabel("Importer :");
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label_4.setBounds(318, 48, 80, 14);
		panel_1.add(label_4);
		
		label_6 = new JLabel("Status");
		label_6.setBounds(408, 49, 133, 14);
		panel_1.add(label_6);
		
		JLabel label_7 = new JLabel("Distributor :");
		label_7.setHorizontalAlignment(SwingConstants.RIGHT);
		label_7.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label_7.setBounds(318, 73, 80, 14);
		panel_1.add(label_7);
		
		label_8 = new JLabel("Status");
		label_8.setBounds(408, 74, 126, 14);
		panel_1.add(label_8);
		
		JLabel label_9 = new JLabel("Retailer :");
		label_9.setHorizontalAlignment(SwingConstants.RIGHT);
		label_9.setFont(new Font("Tahoma", Font.PLAIN, 13));
		label_9.setBounds(318, 98, 80, 14);
		panel_1.add(label_9);
		
		label_10 = new JLabel("Status");
		label_10.setBounds(408, 99, 126, 14);
		panel_1.add(label_10);
		
		JPanel panel_7 = new JPanel();
		sl_panel.putConstraint(SpringLayout.NORTH, panel_7, 6, SpringLayout.SOUTH, panel_1);
		sl_panel.putConstraint(SpringLayout.WEST, panel_7, 10, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, panel_7, -10, SpringLayout.SOUTH, panel);
		sl_panel.putConstraint(SpringLayout.EAST, panel_7, 0, SpringLayout.EAST, panel_6);
		panel_7.setBorder(new TitledBorder(null, "Step 3: Game Control", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(panel_7);
		
		JButton btnStepWeek = new JButton("Step Week");
		btnStepWeek.setAction(action_2);
		
		JLabel lblCustomerDemandWhite = new JLabel("Customer Demand White");
		
		JLabel lblCustomerDemandRed = new JLabel("Customer Demand Red");
		
		textField = new JTextField();
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		JPanel panel_8 = new JPanel();
		panel_8.setBorder(new TitledBorder(null, "Historical Demand", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JLabel lblCurrentWeek = new JLabel("Current Week");
		
		textField_2 = new JTextField();
		textField_2.setEnabled(false);
		textField_2.setColumns(10);
		GroupLayout gl_panel_7 = new GroupLayout(panel_7);
		gl_panel_7.setHorizontalGroup(
			gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_7.createSequentialGroup()
					.addGroup(gl_panel_7.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel_7.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_7.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_7.createSequentialGroup()
									.addComponent(lblCustomerDemandWhite)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_7.createSequentialGroup()
									.addComponent(lblCustomerDemandRed)
									.addGap(18)
									.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.UNRELATED))
						.addGroup(Alignment.TRAILING, gl_panel_7.createSequentialGroup()
							.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnStepWeek, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
							.addGap(48))
						.addGroup(gl_panel_7.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblCurrentWeek)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGap(10)
					.addComponent(panel_8, GroupLayout.PREFERRED_SIZE, 380, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(252, Short.MAX_VALUE))
		);
		gl_panel_7.setVerticalGroup(
			gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_7.createSequentialGroup()
					.addGroup(gl_panel_7.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_7.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_7.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblCustomerDemandWhite)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel_7.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblCustomerDemandRed)
								.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel_7.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblCurrentWeek)
								.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(21)
							.addComponent(btnStepWeek))
						.addComponent(panel_8, GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
					.addContainerGap())
		);
		panel_8.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_8.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Week", "Cust. D.", "Retail O.", "Dist. O.", "Import. O.", "Winery P."
			}
		));
		scrollPane.setViewportView(table);
		panel_7.setLayout(gl_panel_7);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("Shipping", null, panel_3, null);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Inventory", null, panel_2, null);
		
		JPanel panel_4 = new JPanel();
		tabbedPane.addTab("Summary Satistics", null, panel_4, null);
		
		JPanel panel_5 = new JPanel();
		springLayout.putConstraint(SpringLayout.NORTH, panel_5, 637, SpringLayout.NORTH, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, tabbedPane, -6, SpringLayout.NORTH, panel_5);
		springLayout.putConstraint(SpringLayout.WEST, panel_5, 0, SpringLayout.WEST, menuBar);
		springLayout.putConstraint(SpringLayout.SOUTH, panel_5, 0, SpringLayout.SOUTH, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, panel_5, 0, SpringLayout.EAST, menuBar);
		
		JMenu mnNewMenu = new JMenu("DB Connection");
		menuBar.add(mnNewMenu);
		panel_5.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		frmWineGameWine.getContentPane().add(panel_5);
		panel_5.setLayout(null);
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(10, 11, 88, 14);
		panel_5.add(lblStatus);
		
		JLabel lblTheWineGame = new JLabel("The Wine Game - Administrator Module");
		springLayout.putConstraint(SpringLayout.NORTH, tabbedPane, 6, SpringLayout.SOUTH, lblTheWineGame);
		springLayout.putConstraint(SpringLayout.EAST, lblTheWineGame, 295, SpringLayout.WEST, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, lblTheWineGame, 6, SpringLayout.SOUTH, menuBar);
		springLayout.putConstraint(SpringLayout.WEST, lblTheWineGame, 10, SpringLayout.WEST, frmWineGameWine.getContentPane());
		lblTheWineGame.setFont(new Font("Tahoma", Font.PLAIN, 16));
		frmWineGameWine.getContentPane().add(lblTheWineGame);
		JLabel lblNewJgoodiesTitle = new JLabel("Week");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewJgoodiesTitle, 6, SpringLayout.SOUTH, menuBar);
		lblNewJgoodiesTitle.setFont(new Font("Tahoma", Font.PLAIN, 16));
		frmWineGameWine.getContentPane().add(lblNewJgoodiesTitle);
		
		textField_4 = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, lblNewJgoodiesTitle, -8, SpringLayout.WEST, textField_4);
		springLayout.putConstraint(SpringLayout.WEST, textField_4, 737, SpringLayout.WEST, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, textField_4, -10, SpringLayout.EAST, frmWineGameWine.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, textField_4, 6, SpringLayout.SOUTH, menuBar);
		
		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);
		
		JMenuItem mntmAboutWineGame = new JMenuItem("About Wine Game");
		mntmAboutWineGame.setAction(action_3);
		mnAbout.add(mntmAboutWineGame);
		textField_4.setEditable(false);
		frmWineGameWine.getContentPane().add(textField_4);
		textField_4.setColumns(10);
		
	}

	private void upStatplayers() {
		      con_dist = false;
			    con_ret = false;
			     ord_win = true;
			    ord_imp = true;
			    ord_dist = true;
			    ord_ret = true;
		textField_2.setText("" + values.getWeek());
		try {
			connect.pop_tablaOrd(table);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		textField_4.setText(""+values.getWeek());
		 try {
			  String query = "SELECT TIME_TO_SEC(TIMEDIFF(NOW(), rep_win)), TIME_TO_SEC(TIMEDIFF(NOW(), rep_imp)), TIME_TO_SEC(TIMEDIFF(NOW(), rep_dist)), TIME_TO_SEC(TIMEDIFF(NOW(), rep_ret)) FROM games WHERE (ID=?)";
			  Connection conn =  connect.connect_DB();
			  PreparedStatement ps = conn.prepareStatement(query); 
			  ps.setInt(1,values.getIDGame());
			  ResultSet rs = ps.executeQuery();
			  while ( rs.next() ) {
				 if (rs.getDouble(1)<30) con_win=true;
				 if (rs.getDouble(2)<30) con_imp=true;
				 if (rs.getDouble(3)<30) con_dist=true;
				 if (rs.getDouble(4)<30) con_ret=true;
			  }
			  rs.close();
			  ps.close();
			 
			  query = "SELECT Winery, Importer, Distributor, Retailer FROM game_status WHERE (Game_ID=?)";
			  ps = conn.prepareStatement(query); 
			  ps.setInt(1,values.getIDGame());
			   rs = ps.executeQuery();
			  while ( rs.next() ) {
				 if (rs.getInt(1)==1){
					 ord_win=true; 
					 } else{ 
				    ord_win=false;
				 }
				 if (rs.getInt(2)==1) {
					 ord_imp=true;
			  } else { 
				  ord_imp=false;
				 }
				 if (rs.getInt(3)==1) {
					 ord_dist=true;
		        } else{ 
			 ord_dist=false;
			   }
				 if (rs.getInt(4)==1) {
					 ord_ret=true;
				 } else{ 
					 ord_ret=false;
				 }
			  }
			  rs.close();
			  ps.close();
			  conn.close();
		   	  
		 } catch (SQLException e) {
				e.printStackTrace();
		          JOptionPane.showMessageDialog( null,
		       "Error in DB connection!" + e,
		       "Error", JOptionPane.ERROR_MESSAGE);
		    }
		 if (con_win) {
			 lblStatus_1.setForeground(Color.GREEN);
			 lblStatus_1.setText("Online");
		 } else {
			 lblStatus_1.setForeground(Color.RED);
			 lblStatus_1.setText("OFF-line");
		 }
		 if (con_imp) {
			 label_1.setForeground(Color.GREEN);
			 label_1.setText("Online");
		 } else {
			 label_1.setForeground(Color.RED);
			 label_1.setText("OFF-line");
		 }
		 if (con_dist) {
			 label_3.setForeground(Color.GREEN);
			 label_3.setText("Online");
		 } else {
			 label_3.setForeground(Color.RED);
			 label_3.setText("OFF-line");
		 }
		 if (con_ret) {
			 label_5.setForeground(Color.GREEN);
			 label_5.setText("Online");
		 } else {
			 label_5.setForeground(Color.RED);
			 label_5.setText("OFF-line");
		 }
		 
		 if (ord_win) {
			 label_2.setForeground(Color.GREEN);
			 label_2.setText("Orders Generated");
		 } else {
			 label_2.setForeground(Color.RED);
			 label_2.setText("Processing");
		 }
		 if (ord_imp) {
			 label_6.setForeground(Color.GREEN);
			 label_6.setText("Orders Generated");
		 } else {
			 label_6.setForeground(Color.RED);
			 label_6.setText("Processing");
		 }
		 if (ord_dist) {
			 label_8.setForeground(Color.GREEN);
			 label_8.setText("Orders Generated");
		 } else {
			 label_8.setForeground(Color.RED);
			 label_8.setText("Processing");
		 }
		 if (ord_ret) {
			 label_10.setForeground(Color.GREEN);
			 label_10.setText("Orders Generated");
		 } else {
			 label_10.setForeground(Color.RED);
			 label_10.setText("Processing");
		 } 
			
	}
	
	 private static String fName = "Game.config";
	 private JTextField textField_4;
	 private final Action action = new SwingAction();
	 public Timer timer;
	 private JLabel lblStatus_1;
	 private JLabel label_1;
	 private JLabel label_3;
	 private JLabel label_5;
	 private JLabel label_2;
	 private JLabel label_6;
	 private JLabel label_8;
	 private JLabel label_10;
	 
	 private JLabel lblNewLabel;
	 private final Action action_1 = new SwingAction_1();
	 private final Action action_2 = new SwingAction_2();
	 private JTextField textField;
	 private JTextField textField_1;
	 private JTable table;
	 private JTextField textField_2;
	 private boolean con_win;
	 private boolean con_imp;
	 private boolean con_dist;
		private boolean con_ret;
		private  boolean ord_win;
		private boolean ord_imp;
		private boolean ord_dist;
		private boolean ord_ret;
		private final Action action_3 = new SwingAction_3();

	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Create Game");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			create_game juego = new create_game();
			juego.setVisible(true);
			juego.addWindowListener(new WindowListener() {
				@Override
				public void windowActivated(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowClosed(WindowEvent arg0) {
					// TODO Auto-generated method stub
					upStatplayers();
					 lblNewLabel.setText(""+values.getNGame());
				}
				@Override
				public void windowClosing(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowDeactivated(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowDeiconified(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowIconified(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowOpened(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
	        });
	          
		}
	}

	 class RemindTask extends TimerTask {

		    public void run() {
		    	if (values.getIDGame()!=0) {
		    	upStatplayers();
		    	}
		    }
		    
		  }
	
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "Open Existing Game");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			sel_game sel = new sel_game();
			sel.setVisible(true);
			sel.addWindowListener(new WindowListener() {
				@Override
				public void windowActivated(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowClosed(WindowEvent arg0) {
					// TODO Auto-generated method stub
					upStatplayers();
					 lblNewLabel.setText(""+values.getNGame());
				}
				@Override
				public void windowClosing(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowDeactivated(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowDeiconified(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowIconified(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void windowOpened(WindowEvent arg0) {
					// TODO Auto-generated method stub
					
				}
	        });
	          
		}
	}
	private class SwingAction_2 extends AbstractAction {
		public SwingAction_2() {
			putValue(NAME, "Step Week");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			if (ord_win && ord_imp && ord_dist && ord_ret) {
				try {
					
			    Connection conn =  connect.connect_DB();
			    
		  		String query = "UPDATE game_status SET week=?, Winery=?, Importer=?, Distributor=?, Retailer=? WHERE Game_ID=?";
		  		PreparedStatement  ps=conn.prepareStatement(query);
		  		  ps.setInt(1, values.getWeek()+1);
		  		  ps.setInt(2, 0);
		  		  ps.setInt(3, 0);
		  		  ps.setInt(4, 0);
		  		  ps.setInt(5, 0);
		  		  ps.setInt(6, values.getIDGame());
		  		  ps.execute();
		  		  ps.close();
		  		  
		  		query ="INSERT INTO orders (Game_ID, Level, Semana, Order_Red, Order_White) VALUES (?,?,?,?,?)";
		  		  ps=conn.prepareStatement(query);
		  		  ps.setInt(1, values.getIDGame());
		  		  ps.setInt(2, 4);
		  		  ps.setInt(3, values.getWeek()+1);
		  		  ps.setInt(4, Integer.parseInt(textField.getText().toString()));
		  		  ps.setInt(5, Integer.parseInt(textField_1.getText().toString()));
		  		  ps.execute();
		  		  ps.close();
		  		  
		  		for (int i=0;i<4;i++) {
		  			int[] demand = new int[2];
		  			int[] ship = new int[2];
					int[] invback = new int [4];
					int[] rec = new int[2];
					int[] pinvback = new int [4]; 
		  		
		  		String query2 = "Select Ship_Red, Ship_white FROM shipping WHERE ((Game_ID=?) AND (Semana=?) AND (Level=?))";
		  		PreparedStatement  ps2=conn.prepareStatement(query2);
		  		ps2.setInt(1, values.getIDGame());
		  		ps2.setInt(2, values.getWeek()+1);
		  		ps2.setInt(3, i);
		  		ResultSet rs = ps2.executeQuery();
		  		while ( rs.next() ) {
		  			  ship[0] = rs.getInt(1);
		  			  ship[1] = rs.getInt(2);
		  		  }
		  		rs.close();
		  		ps2.close();
		  		
		  		query2 = "Select Order_red, Order_white FROM orders WHERE ((Game_ID=?) AND (Semana=?) AND (Level=?))";
		  		  ps2=conn.prepareStatement(query2);
		  		ps2.setInt(1, values.getIDGame());
		  		ps2.setInt(2, values.getWeek());
		  		ps2.setInt(3, i+1);
		  		 rs = ps2.executeQuery();
		  		while ( rs.next() ) {
		  			  demand[0] = rs.getInt(1);
		  			  demand[1] = rs.getInt(2);
		  		}
		  		rs.close();
		  		ps2.close();
		  		if (i!=0) {
		  		query2 = "Select red_Ship, white_ship FROM orders_inv_back WHERE ((Game_ID=?) AND (ava_sem=?) AND (Level=?))";
		  		  ps2=conn.prepareStatement(query2);
		  		ps2.setInt(1, values.getIDGame());
		  		ps2.setInt(2, values.getWeek());
		  		ps2.setInt(3, i-1);
		  		rs = ps2.executeQuery();
		  		while ( rs.next() ) {
		  			  rec[0] = rs.getInt(1);
		  			  rec[1] = rs.getInt(2);
		  		}
		  		rs.close();
		  		ps2.close();
		  		} else {
		  			query2 = "Select Bot_Red, Bot_white FROM production WHERE ((Game_ID=?) AND (Semana=?))";
			  		ps2= conn.prepareStatement(query2);
			  		ps2.setInt(1, values.getIDGame());
			  		ps2.setInt(2, values.getWeek()-1);
			  		 rs = ps2.executeQuery();
			  		while ( rs.next() ) {
			  			  rec[0] = rs.getInt(1);
			  			  rec[1] = rs.getInt(2);
		  	
		  		}
			  		rs.close();
			  		ps2.close();
			  		}
		  		invback = connect.Invback(i);
		  		if (demand[0]>ship[0]) {
		  			pinvback[1] = demand[0]- ship[0] + invback[1];
		  			pinvback[0] = (rec[0] + invback[0]) - ship[0];
		  		} else if (ship[0]>=demand[0]) {
		  			pinvback[1] = demand[0]- ship[0] + invback[1];
		  			pinvback[0] = (rec[0] + invback[0]) - ship[0];
		  		}
		  		if (demand[1]>ship[1]) {
		  			pinvback[3] = demand[1]- ship[1] + invback[3];
		  			pinvback[2] = (rec[1] + invback[2]) - ship[1];
		  		} else if (ship[1]>=demand[1]) {
		  			pinvback[3] = demand[1]- ship[1] + invback[3];
		  			pinvback[2] = (rec[1] + invback[2]) - ship[1];
		  		}
		  		
		  		
		
		  		 query ="INSERT INTO orders_inv_back (Game_ID, Level, Semana, ava_sem, Red_ship,Red_Inv,Red_back,white_ship,White_Inv,White_Back) VALUES (?,?,?,?,?,?,?,?,?,?)";
		  		 ps=conn.prepareStatement(query);
		  		 ps.setInt(1, values.getIDGame());
		  		 ps.setInt(2, i);
		  		 ps.setInt(3, values.getWeek()+1);
		  		 if (i==0){
		  			ps.setInt(4, values.getWeek()+5);
		  		 } else {
		  			ps.setInt(4, values.getWeek()+3);
		  		 }
		  		ps.setInt(5,ship[0]);
		  		ps.setInt(6,pinvback[0]);
		  		ps.setInt(7,pinvback[1]);
		  		ps.setInt(8,ship[1]);
		  		ps.setInt(9,pinvback[2]);
		  		ps.setInt(10,pinvback[3]);
		  		ps.execute();
		  		ps.close();
		  		System.out.println("level " + i + " demand :" +demand[0] + " shiping " + ship[0] + " invent "+ invback[0] + " back " +invback[1] + " received " +rec[0]);
		  		
		  		}
		  		int week = values.getWeek()+1;
		  		values.setWeek(week);
		  		JOptionPane.showMessageDialog( null,
					       "Week set to "+week,
					       "Change", JOptionPane.INFORMATION_MESSAGE);
		  		conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			} else {
				JOptionPane.showMessageDialog( null,
					       "Players have not finished their orders!",
					       "Error", JOptionPane.ERROR_MESSAGE);
			}
			
		}
	}
	private class SwingAction_3 extends AbstractAction {
		public SwingAction_3() {
			putValue(NAME, "About Wine Game");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			splash spl = new splash();
		}
	}
}
